package com.mc2.docapp.disabilityassessmenttool.utils;

import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMailTLS implements Runnable {
    private static Session session;
    private static Properties props;
    private static SendMailTLS sendMailTLS = null;

    static {
        props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("docappmc2@gmail.com", "saurabhnew");
                    }
                });
    }

    private Message message;

    private SendMailTLS() {
    }

    public static void main(String[] args) {
        SendMailTLS.getMailer().sendMail(new MailerObject("saurabh.bhum.kar@gmail.com", "subject", "content"));
    }

    public static SendMailTLS getMailer() {
        if (sendMailTLS == null) {
            sendMailTLS = new SendMailTLS();
        }
        return sendMailTLS;
    }

    public void sendMail(MailerObject mailerObject) {
        try {
            message = new MimeMessage(session);
            message.setFrom(new InternetAddress("docappmc2@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("saurabh.bhumkar@gmail.com" + "," + mailerObject.getTo_address()));
            message.setSubject(mailerObject.getSubject());
            message.setText(mailerObject.getContent());
            new Thread(this).start();
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void run() {
        try {
            Transport.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendMail(MailerObject mailerObject, boolean b) {
        try {
            // Create a default MimeMessage object.
            message = new MimeMessage(session);

            // Set From: header field of the header.
            message.setFrom(new InternetAddress("docappmc2@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("saurabh.bhumkar@gmail.com" + "," + mailerObject.getTo_address()));
            message.setSubject(mailerObject.getSubject());
            message.setText(mailerObject.getContent());


            // Create the message part
            BodyPart messageBodyPart = new MimeBodyPart();

            // Now set the actual message
            messageBodyPart.setText("Disability Report Generated at "+ new Date().toLocaleString());

            // Create a multipar message
            Multipart multipart = new MimeMultipart();

            // Set text message part
            multipart.addBodyPart(messageBodyPart);

            // Part two is attachment
            messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(mailerObject.getContent());
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(source.getName());
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
            new Thread(this).start();

            System.out.println("Sent message successfully....");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}